package cn.bgs.controller;

import cn.bgs.pojo.Areas;
import cn.bgs.pojo.Cities;
import cn.bgs.pojo.Provinces;
import cn.bgs.pojo.Student;
import cn.bgs.service.StudentService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class StudentController {
    Logger log= LogManager.getLogger();
    @Autowired private StudentService studentService;


    @RequestMapping("/start")
    public String find(){
        return "home";
    }



    @RequestMapping("/findAll")
    public String findAll(@RequestParam(defaultValue = "1")Integer pageNum, Model m,Student students){

        Integer pageSize=4;

        PageInfo<Student> list= studentService.stuQuery(pageNum,pageSize,students);

        m.addAttribute("p",list);

        return "/student/success";

    }


    @RequestMapping("/deleteByPrimaryKey")

    public String deleteByPrimaryKey(Integer[] ids){

        for( Integer stuId:ids){

           int ff= studentService.deleteByPrimaryKey(stuId);
           
        }

        return "redirect:/findAll";

    }
    @RequestMapping("/deleteDan")
    public String deleteDan(Integer stuId){

        studentService.deleteByPrimaryKey(stuId);

        return "redirect:/findAll";
    }



/*
    @RequestMapping("/selectByPrimaryKey")
    public String selectByPrimaryKey(Model m,Integer stuId){

        Student student=studentService.selectByPrimaryKey(stuId);


        m.addAttribute("list",student);

        List<Provinces> list=studentService.findAllProcess();
        m.addAttribute("allPro",list);

          List<Areas> ff=studentService.findAllAreas();
        m.addAttribute("allAre",ff);

        List<Cities> tt=studentService.findAllCities();
        m.addAttribute("allCiti",tt);


        return "student/studentModify";


    }*/
    @RequestMapping("/updateById")
    public String updateById(Integer stuId,Model m ){

        m.addAttribute("stuId",stuId);

        return "/student/stuAjaxModify";

    }

    @RequestMapping("/provinces")
    @ResponseBody
    public Map<String,Object> provinces(){

        List<Provinces> p=studentService.findAllProcess();

        List<Cities> c=studentService.findAllCities();

        List<Areas> a = studentService.findAllAreas();

        Map<String,Object> map =new HashMap<>();

        map.put("a",a);
        map.put("c",c);
        map.put("p",p);


        return map;
    }




    @RequestMapping("/updateById1")
    @ResponseBody
    public Map<String,Object> updateById1(@RequestBody Student student){
        Map<String,Object> m = new HashMap<>();
        Student an=studentService.selectByPrimaryKey(student.getStuId());
        m.put("an",an);

        //map集合转换为JSON对象 /需要一个pom 文件 fastjson 处理字符串
        JSONObject result = JSONObject.parseObject(JSON.toJSONString(m));
        //将JSON对象传递给前端AJAX接收
        return result;
    }

    @RequestMapping("/updateStudent")
    @ResponseBody
    public int updateStudent(@RequestBody Student student){
        int ppp= studentService.updateByPrimaryKeySelective(student);
        return ppp;

    }




/*    @RequestMapping("/updateByPrimaryKeySelective")
    public String updateByPrimaryKeySelective(Student record){

        int ff=studentService.updateByPrimaryKeySelective(record);

        return "redirect:/findAll";

    }*/


    @RequestMapping("/addTz")
    public String addTz(Model m){

        List<Provinces> list=studentService.findAllProcess();
        m.addAttribute("allPro",list);

        List<Areas> ff=studentService.findAllAreas();
        m.addAttribute("allAre",ff);

        List<Cities> tt=studentService.findAllCities();
        m.addAttribute("allCiti",tt);

        return "student/addStudent";

    }



    @RequestMapping("/insertSelective")
    public String insertSelective(Student record ){

        int hh=studentService.insertSelective(record);

        return "redirect:/findAll";


    }



    @RequestMapping("/stuQuery")
    public String stuQuery(Student students,Model m){
        log.info("stuQuery——————————————————————"+students);

        Integer pageSize = 4;
        PageInfo<Student> page =  studentService.stuQuery(1,pageSize,students);


        m.addAttribute("p",page);
        return "/student/success";
    }


}
